package com.icode.bq;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @Author: my_code
 * @Email: my_code@foxmail.com
 * @Create: 2020-12-12 19:18
 */
public class SynchronousQueueTest {
    public static void main(String[] args) {
        BlockingQueue<Integer> queue = new SynchronousQueue<>();

        new Thread(() -> {


            //延时几秒
            try {
                System.out.println(Thread.currentThread().getName() + "put:" + 1);
                queue.put(1);
                TimeUnit.SECONDS.sleep(2);

                System.out.println(Thread.currentThread().getName() + "put:" + 2);
                queue.put(2);
                TimeUnit.SECONDS.sleep(2);

                System.out.println(Thread.currentThread().getName() + "put:" + 3);
                queue.put(3);
                TimeUnit.SECONDS.sleep(2);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "T1").start();

        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName() + "take:" + queue.take());
                System.out.println(Thread.currentThread().getName() + "take:" + queue.take());
                System.out.println(Thread.currentThread().getName() + "take:" + queue.take());

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "T2").start();
    }
}
